<template>
  <div class="app-container">
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"> </el-input>

    <el-tree
      class="filter-tree"
      :data="subjectList"
      :props="defaultProps"
      default-expand-all
      :filter-node-method="filterNode"
      ref="tree"
    >
    </el-tree>
  </div>
</template>

<script>
import subjectApi from "@/api/edu/subject";
export default {
  data() {
    return {
      subjectList: [],
      defaultProps: {
        children: "child",
        label: "title",
      },
      filterText: "",
    };
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getNestedTreeList();
  },

  methods: {
    // 获取tree数据
    getNestedTreeList() {
      subjectApi.getNestedTreeList().then((response) => {
        //console.log(response)
        this.subjectList = response.data.items;
      });
    },
    filterNode(value, data) {
        if (!value) return true;
        return data.title.toUpperCase().indexOf(value.toUpperCase()) !== -1;
      }
  },
};
</script>